<template>
  <el-drawer
    v-model="props.drawerVisibility"
    :title="title"
    :size="size"
    :before-close="handleClose"
  >
    <el-form ref="ruleFormRef" :model="form" :rules="rules" label-width="10rem">
      <el-form-item v-if="flag" label="地址标识：">
        <el-input disabled v-model.trim="info.id" />
      </el-form-item>
      <el-form-item prop="name" label="分类名称：">
        <el-input v-model="info.name" />
      </el-form-item>
      <el-form-item prop="" label="图标上传：">
        <el-upload
          class="avatar-uploader"
          action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="false" :src="info.avg" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item prop="" label="选中图标：">
        <el-upload
          class="avatar-uploader"
          action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="false" :src="info.act_svg" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item prop="sort" label="分类排序：">
        <el-input v-model="info.sort" />
      </el-form-item>
      <el-form-item>
        <el-button :type="ElButtonType" @click="submit(ruleFormRef)"
          >提交</el-button
        >
        <el-button @click="handleClear">重置</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<script lang="ts" setup>
import { themePinia } from "@/libs/pinia/theme";
import { reactive, ref, computed, PropType } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { ElNotification } from "element-plus";
import { Plus } from '@element-plus/icons-vue'

import { ICategoryForm } from "@/libs/type/category";

const props = defineProps({
  drawerVisibility: {
    type: Boolean,
    default: false,
  },
  title: {
    type: String,
    default: "详细信息",
  },
  size: {
    type: String,
    default: "30%",
  },
  form: {
    type: Object as PropType<ICategoryForm>,
    default: () => ({} as ICategoryForm),
  },
  flag: {
    type: Boolean,
    default: false,
  },
});

let info: ICategoryForm = reactive(props.form);

// 验证规则
const ruleFormRef = ref<FormInstance>();
const rules = reactive<FormRules<any>>({
  name: [
    { required: true, message: "分类名称是必填项！", trigger: "blur" },
    {
      min: 2,
      max: 16,
      message: "分类名称长度应该在2-16之间！",
      trigger: "blur",
    },
  ],
  // sort: [{ type: "number", message: "排序必须为数字类型！", trigger: "blur" }],
});

const handleClear = () => {
  //重置
  info = Object.assign(info, {
    id: undefined,
    name: undefined,
    svg: undefined,
    act_svg: undefined,
    sort: undefined,
  });
};

const emits = defineEmits(["handleCloseEmit", "handleSubmitEmit"]);

const handleClose = () => {
  // 判断是否有值进行关闭
  emits("handleCloseEmit");
};

const submit = async (infos: any) => {
  await infos.validate((valid: any, fields: any) => {
    if (valid) {
      emits("handleSubmitEmit", { ...info });
    } else {
      ElNotification({
        title: "错误",
        message: "This is an error message",
        type: "error",
      });
    }
  });
};

// 上传
const handleAvatarSuccess=()=>{

}

const beforeAvatarUpload=()=>{

}

// 主题
const theme = themePinia();

const ElinputBackgroundColor = computed(() => theme.ElinputBackgroundColor);
const ElinputTextColor = computed(() => theme.ElinputTextColor);
const ElinputHoverColor = computed(() => theme.ElinputHoverColor);
const ElButtonType = computed(() => theme.ElButtonType as any);
</script>

<style scoped lang="scss">
el-row {
  margin-top: 1.2rem;
}

.image-box {
  display: flex;
  height: 100px;
  margin-top: 1.2rem;
}

:deep(.el-form-item) {
  display: flex;
  align-items: center;
}

:deep(.m-2) {
  margin: 0;
}

:deep(.el-form-item__label) {
  font-size: 1.2rem;
  /* margin-top: 0.3rem; */
}

:deep(.el-input__wrapper) {
  //移除主盒子样式
  padding: 0;
  box-shadow: none !important;
  background-color: rgba($color: #000000, $alpha: 0) !important;
  flex-grow: 0;

  .el-input__inner {
    background-color: v-bind(ElinputBackgroundColor);
    color: v-bind(ElinputTextColor);
    border: 1px;
    border-color: #999;
    border-style: solid;
    font-size: 1rem;
    padding: 0.5rem 1rem;
    transition: all 250ms ease;
    border-radius: 0.4rem;

    &:hover {
      border-color: v-bind(ElinputHoverColor);
    }
  }
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
